<template>
	<view class="bgcolor" style="position: relative;">
		<header-top :messageInfo="{typeFlag:true,message:'意见反馈'}">
		</header-top>
		<view class="fankui">
			<view style="margin: 20rpx;">问题和意见</view>
			<u--textarea v-model="value1" style='text-indent: 1em;' placeholder="简要描述你要反馈的问题和意见"></u--textarea>
			<view class="allimage flexleft">
				<view class="flexcenter itemlable" style="flex-wrap: wrap;" v-if="pzimage.length"
					v-for="(item,index) in pzimage" :key="index">
					<view class="itemimage">
						<image :src="item" mode=""></image>
						<view class="deleteIMg" @click="delimage(index)"></view>
					</view>
				</view>
				<view class="itemlable flexcenter">
					<view class="upload flexcenter" @click="upload" v-if="pzimage.length >= 5?false:true">
						<u-icon name='plus'></u-icon>
					</view>
				</view>
			</view>
		</view>
		<view class="contact">
			<view style="margin: 20rpx;">联系方式</view>
			<view class="" style="padding:0 30rpx;">
				<u--input placeholder="请填写联系方式" border="bottom" clearable v-model="nums"></u--input>
			</view>
		</view>
		<view class="flexcenter" style="margin-top: 60rpx; color: #3476fe;text-decoration: underline;" @click="lishifk">历史反馈</view>
		<view
			style="padding: 0 70rpx;position: absolute;left: 0;bottom: 0rpx; width: 100%;box-sizing: border-box;padding-bottom: 30rpx;">
			<view class="buttonStyle flexcenter" @click="submit">提交</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				value1: '',
				nums: '',
				pzimage: []
			}
		},
		methods: {
			// 提交
			submit() {
				if (this.value1 == '') {
					httpRequest.toast('请输入问题和意见');
					return false;
				}
				if (this.nums == '') {
					httpRequest.toast('请输入联系方式');
					return false;
				}
				if (!this.nums.match(/^1\d{10}$/)) {
					httpRequest.toast("请输入有效的联系方式");
					return false;
				}
				httpRequest.request('/employeesapi/user/addFeddback', 'POST', {
					des: this.value1,
					images: this.pzimage.join(),
					mobile: this.nums
				}).then(res => {
					if (res.code == 1) {
						httpRequest.toast(res.msg)
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 1000)
					} else {
						httpRequest.toast(res.msg)
					}
				})
			},
			upload() {
				let _this = this;
				let pzimage = _this.pzimage;
				let num = 5 - pzimage.length;
				if (num <= 0) {
					return false;
				}
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						console.log(res)
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], _this.pzimage)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									console.log(res[i])
									_this.uploadImageService(res[i], _this.pzimage)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {
						console.log(res)
					}
				});
			},
			uploadImageService(url, upImg) {
				console.log(111)
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					upImg.push(image_item);
				}).catch(err => {});
			},
			lishifk(){
				uni.navigateTo({
					url:"/pages/lishifk/lishifk"
				})
			},
			// 删除图片
			delimage(i) {
				this.pzimage.splice(i, 1)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.fankui {
		width: 100%;
		padding: 20rpx;
		background-color: #fff;
		box-sizing: border-box;

	}

	.contact {
		padding: 20rpx;
		margin-top: 20rpx;
		background-color: #fff;
		box-sizing: border-box;
	}

	.upload {
		width: 100rpx;
		height: 100rpx;
		border: 1px solid #999999;
		border-radius: 5rpx;
	}

	::v-deep .u-border {
		border-style: none
	}

	.allimage {
		flex-wrap: wrap;

	}

	.itemlable {
		margin-top: 30rpx;
		width: 25%;

	}

	.itemimage {
		position: relative;

		image {
			width: 100rpx;
			height: 100rpx;
			border-radius: 5rpx;
		}
	}
</style>
